<template>
  <div class="app-container">
    <el-card>
      <div class="search-container">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item prop="keywords" label="名称">
            <el-input
              v-model="queryParams.keywords"
              placeholder="输入名称"
              clearable
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleQuery"
              ><i-ep-search />搜索</el-button
            >
          </el-form-item>

          <el-form-item>
            <el-button type="success" @click="openDialog()">
              <i-ep-plus />新增</el-button
            >
          </el-form-item>

          <el-button
            style="float: right; margin-right: 20px"
            @click="resetQuery"
            ><i-ep-refresh
          /></el-button>
        </el-form>
      </div>
      <el-card shadow="never" class="table-container">
        <el-table
          ref="dataTableRef"
          v-loading="loading"
          :data="tableData"
          highlight-current-row
          :stripe="true"
          size="large"
        >
          <el-table-column
            align="center"
            label="序号"
            type="index"
            width="100"
          />
          <el-table-column align="center" label="ID" prop="id" width="100" />
          <el-table-column align="center" label="名称" prop="name" />
          <el-table-column align="center" label="所属科室" prop="deptName" />
          <el-table-column align="center" label="优先级" prop="priority" />
          <el-table-column
            align="center"
            label="检测时长/min"
            prop="duration"
          />
          <el-table-column align="center" label="发布时间" prop="createTime" />
          <el-table-column align="center" label="修改时间" prop="updateTime" />
          <el-table-column
            align="center"
            fixed="right"
            label="操作"
            width="180"
          >
            <template #default="scope">
              <el-button
                type="primary"
                text
                size="small"
                @click.stop="openDialog(scope.row.id)"
              >
                <i-ep-edit />编辑
              </el-button>
              <el-popconfirm
                title="你确定需要删除?"
                @confirm="handleDelete(scope.row.id)"
              >
                <template #reference>
                  <el-button type="danger" text size="small">
                    <i-ep-delete />删除
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-if="total > 0"
          v-model:total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="handleQuery"
        />
      </el-card>
    </el-card>

    <!-- 体检项目表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="30%"
      @close="closeDialog"
    >
      <el-form
        ref="dataFormRef"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="所属科室" prop="deptId">
          <el-tree-select
            v-model="formData.deptId"
            placeholder="选择上级科室"
            :data="deptOptions"
            filterable
            check-strictly
            :render-after-expand="false"
          />
        </el-form-item>

        <el-form-item label="体检名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入体检名称" />
        </el-form-item>

        <el-form-item label="优先级" prop="priority">
          <el-input v-model="formData.priority" placeholder="请输入优先级" />
        </el-form-item>

        <el-form-item label="时长/min" prop="duration">
          <el-input v-model="formData.duration" placeholder="请输入体检时长" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {
  getPageCheckProjectAPI,
  getFormCheckProjectAPI,
  addCheckProjectAPI,
  updateCheckProjectAPI,
  deleteCheckProjectAPI,
} from "@/api/checkProject/index";

import { getFirstDeptOptions } from "@/api/dept";

import { IPage, ICheckProject } from "@/api/checkProject/types";

import { ref, reactive } from "vue";

const total = ref(0);
const loading = ref(false);
const tableData = ref<ICheckProject[]>([]);
const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);
const deptOptions = ref<OptionType[]>();
// 表单数据
const formData = reactive<ICheckProject>({});
// 表单规则
const rules = reactive({
  deptId: [{ required: true, message: "请选择项目所属科室", trigger: "blur" }],
  name: [{ required: true, message: "请输入项目名称", trigger: "blur" }],
  priority: [{ required: true, message: "请输入项目优先级", trigger: "blur" }],
  duration: [
    { required: true, message: "请输入项目测试评时长", trigger: "blur" },
  ],
});

const dialog = reactive<DialogOption>({
  visible: false,
});
const queryParams = reactive<IPage>({
  pageNum: 1,
  pageSize: 10,
});

onMounted(() => {
  handleQuery();
});

/** 获取科室下拉数据  */
async function loadDeptOptions() {
  getFirstDeptOptions().then((response) => {
    deptOptions.value = response.data;
  });
}

// 获取体检项目列表
function handleQuery() {
  loading.value = true;
  getPageCheckProjectAPI(queryParams)
    .then(({ data }) => {
      tableData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 刷新查询 */

function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 关闭表单弹窗 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

/** 打开体检项目表单弹窗 */
function openDialog(id?: number) {
  loadDeptOptions();
  if (id) {
    dialog.title = "修改体检项目";
    getFormCheckProjectAPI(id).then(({ data }) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增体检项目";
  }
  dialog.visible = true;
}

/** 体检项目保存提交 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
        updateCheckProjectAPI(id, formData)
          .then(() => {
            ElMessage.success("修改成功");
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        addCheckProjectAPI(formData)
          .then(() => {
            ElMessage.success("新增成功");
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/** 刷新表单 */
function resetForm() {
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
}
/** 删体检项目 */
function handleDelete(id: number) {
  loading.value = true;
  deleteCheckProjectAPI(id)
    .then(() => {
      ElMessage.success("删除成功");
      resetQuery();
    })
    .finally(() => (loading.value = false));
  resetForm();
}
</script>

<style lang="scss" scoped></style>
